package com.qiandao.xuexitong.handler;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.HashMap;
import java.util.Map;

public class AttendanceExportHandler implements SheetWriteHandler {
    
    private final Map<String, CellStyle> statusStyles = new HashMap<>();
    private final String className;
    private final String teacherName;
    private final Integer signedCount;
    private final String startTime;
    private final String endTime;
    private final String signMethod;
    
    public AttendanceExportHandler(Workbook workbook, String className, String teacherName, 
                                 Integer signedCount, String startTime, String endTime, String signMethod) {
        this.className = className;
        this.teacherName = teacherName;
        this.signedCount = signedCount;
        this.startTime = startTime;
        this.endTime = endTime;
        this.signMethod = signMethod;
        
        // 创建迟到样式（黄色背景）
        CellStyle lateStyle = workbook.createCellStyle();
        lateStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        lateStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        lateStyle.setAlignment(HorizontalAlignment.CENTER);
        lateStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        
        // 创建已签到样式（绿色背景）
        CellStyle successStyle = workbook.createCellStyle();
        successStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        successStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        successStyle.setAlignment(HorizontalAlignment.CENTER);
        successStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        
        // 创建缺勤样式（红色背景）
        CellStyle absentStyle = workbook.createCellStyle();
        absentStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        absentStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        absentStyle.setAlignment(HorizontalAlignment.CENTER);
        absentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        
        // 创建未签到样式（灰色背景）
        CellStyle unsignedStyle = workbook.createCellStyle();
        unsignedStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        unsignedStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        unsignedStyle.setAlignment(HorizontalAlignment.CENTER);
        unsignedStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        
        // 注册状态样式
        statusStyles.put("迟到", lateStyle);
        statusStyles.put("已签到", successStyle);
        statusStyles.put("缺勤", absentStyle);
        statusStyles.put("未签到", unsignedStyle);
    }
    
    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        // 不需要实现
    }
    
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        
        // 创建标题样式
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        titleStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font titleFont = workbook.createFont();
        titleFont.setBold(true);
        titleFont.setFontHeightInPoints((short) 16);
        titleStyle.setFont(titleFont);
        
        // 创建摘要样式（蓝色背景）
        CellStyle summaryStyle = workbook.createCellStyle();
        summaryStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        summaryStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        summaryStyle.setAlignment(HorizontalAlignment.LEFT);
        summaryStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        
        // 设置标题
        Row titleRow = sheet.createRow(0);
        titleRow.setHeight((short) 600);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue("签到");
        titleCell.setCellStyle(titleStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        
        // 设置摘要信息
        Row summaryRow1 = sheet.createRow(1);
        Cell cell1 = summaryRow1.createCell(0);
        cell1.setCellValue(String.format("班级：%s    教师：%s", className, teacherName));
        cell1.setCellStyle(summaryStyle);
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        
        Row summaryRow2 = sheet.createRow(2);
        Cell cell2 = summaryRow2.createCell(0);
        cell2.setCellValue(String.format("已签到人数：%d    开始时间：%s", signedCount, startTime));
        cell2.setCellStyle(summaryStyle);
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7));
        
        Row summaryRow3 = sheet.createRow(3);
        Cell cell3 = summaryRow3.createCell(0);
        cell3.setCellValue(String.format("签到方式：%s    结束时间：%s", signMethod, endTime));
        cell3.setCellStyle(summaryStyle);
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7));
        
        // 设置列宽
        for (int i = 0; i < 8; i++) {
            sheet.setColumnWidth(i, 20 * 256);
        }
    }
    
    public CellStyle getStatusStyle(String status) {
        return statusStyles.get(status);
    }
} 